<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>执行一个正则表达式搜索并且使用一个回调进行替换</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.preg-replace-callback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_replace_callback</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.5, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">preg_replace_callback</span> &mdash; <span class="dc-title">执行一个正则表达式搜索并且使用一个回调进行替换</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-replace-callback-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_replace_callback</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$subject</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$count</code></span>
  ]] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
  这个函数的行为除了
  可以指定一个 <code class="parameter">callback</code> 替代 <code class="parameter">replacement</code> 进行替换
  字符串的计算，其他方面等同于 <span class="function"><a href="preg_replace.html" class="function">preg_replace()</a></span>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-replace-callback-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">pattern</code></dt>

     <dd>

      <p class="para">
      要搜索的模式，可以是字符串或一个字符串数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
      一个回调函数，在每次需要替换时调用，调用时函数得到的参数是从<code class="parameter">subject</code>
      中匹配到的结果。回调函数返回真正参与替换的字符串。这是该回调函数的签名：
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">handler</span></span>
         ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$matches</code></span>
        ) : <span class="type">string</span></div>

      </p>
      <p class="para">
      你可能经常会需要<code class="parameter">callback</code>函数而
      仅用于<span class="function"><strong>preg_replace_callback()</strong></span>一个地方的调用。在这种情况下，你可以
      使用<a href="functions.anonymous.html" class="link">匿名函数</a>来定义一个匿名函数作
      为<span class="function"><strong>preg_replace_callback()</strong></span>调用时的回调。 这样做你可以保留所有
      调用信息在同一个位置并且不会因为一个不在任何其他地方使用的回调函数名称而污染函数名称空间。
      </p>
      <p class="para">
       <div class="example" id="example-5869">
        <p><strong>Example #1 <span class="function"><strong>preg_replace_callback()</strong></span> 和 
        匿名函数</strong></p>
        <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;一个unix样式的命令行过滤器，用于将段落开始部分的大写字母转换为小写。&nbsp;*/<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://stdin"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">)&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"can't&nbsp;read&nbsp;stdin"</span><span style="color: #007700">);<br />while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'|&lt;p&gt;\s*\w|'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
        </div>

       </div>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">subject</code></dt>

     <dd>

      <p class="para">
       要搜索替换的目标字符串或字符串数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">limit</code></dt>

     <dd>

      <p class="para">
      对于每个模式用于每个 <code class="parameter">subject</code> 字符串的最大可替换次数。
      默认是<em>-1</em>（无限制）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">count</code></dt>

     <dd>

      <p class="para">
      如果指定，这个变量将被填充为替换执行的次数。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-replace-callback-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  如果<code class="parameter">subject</code>是一个数组，
  <span class="function"><strong>preg_replace_callback()</strong></span>返回一个数组，其他情况返回字符串。
  错误发生时返回 <strong><code>NULL</code></strong>。
  </p>
  <p class="para">
  如果查找到了匹配，返回替换后的目标字符串（或字符串数组）， 其他情况<code class="parameter">subject</code>
  将会无变化返回。 
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.preg-replace-callback-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        增加了参数<code class="parameter">count</code>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-replace-callback-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5870">
    <p><strong>Example #2 <span class="function"><strong>preg_replace_callback()</strong></span>示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;将文本中的年份增加一年.<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"April&nbsp;fools&nbsp;day&nbsp;is&nbsp;04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Last&nbsp;christmas&nbsp;was&nbsp;12/24/2001\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;回调函数<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">next_year</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;通常:&nbsp;$matches[0]是完成的匹配<br />&nbsp;&nbsp;//&nbsp;$matches[1]是第一个捕获子组的匹配<br />&nbsp;&nbsp;//&nbsp;以此类推<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]+</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br />echo&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"|(\d{2}/\d{2}/)(\d{4})|"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"next_year"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
April fools day is 04/01/2003
Last christmas was 12/24/2002
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5871">
    <p><strong>Example #3 <span class="function"><strong>preg_replace_callback()</strong></span>使用递归构造处理BB码的封装</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"plain&nbsp;[indent]&nbsp;deep&nbsp;[indent]&nbsp;deeper&nbsp;[/indent]&nbsp;deep&nbsp;[/indent]&nbsp;plain"</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">parseTagsRecursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;译注:&nbsp;对此正则表达式分段分析<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;首尾两个#是正则分隔符<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\[indent]&nbsp;匹配一个原文的[indent]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;((?:[^[]|\[(?!/?indent])|(?R))+)分析:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;(?:[^[]|\[(?!/?indent])分析:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;首先它是一个非捕获子组<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;两个可选路径,&nbsp;一个是非[字符,&nbsp;另一个是[字符但后面紧跟着不是/indent或indent.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;(?R)&nbsp;正则表达式递归<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\[/indent]&nbsp;匹配结束的[/indent]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;$regex&nbsp;=&nbsp;'#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(is_array($input))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$input&nbsp;=&nbsp;'&lt;div&nbsp;style="margin-left:&nbsp;10px"&gt;'.$input[1].'&lt;/div&gt;';<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;preg_replace_callback($regex,&nbsp;'parseTagsRecursive',&nbsp;$input);<br />}<br /><br />$output&nbsp;=&nbsp;parseTagsRecursive($input);<br /><br />echo&nbsp;$output;<br />?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-replace-callback-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pcre.pattern.html" class="link">PCRE 模式</a></li>
    <li class="member"><span class="function"><a href="preg_replace_callback_array.html" class="function" rel="rdfs-seeAlso">preg_replace_callback_array()</a> - Perform a regular expression search and replace using callbacks</span></li>
    <li class="member"><span class="function"><a href="preg_quote.html" class="function" rel="rdfs-seeAlso">preg_quote()</a> - 转义正则表达式字符</span></li>
    <li class="member"><span class="function"><a href="preg_replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - 执行一个正则表达式的搜索和替换</span></li>
    <li class="member"><span class="function"><a href="preg_last_error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - 返回最后一个PCRE正则执行产生的错误代码</span></li>
    <li class="member"><a href="functions.anonymous.html" class="link">匿名函数</a></li>
    <li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>